Java Web实验一

世上本没有路,走的人多了,便成了路。

文学家鲁迅

实验内容


  • 1.建立名为login.html页面,并在web.xml中将login.html配置为欢迎页面。其运行结果如下图所示。点击“登陆”按钮之后,请求LoginServlet对其处理。
  • 2.编写名为LoginServlet的Servlet,取得表单传递的参数(账号和密码)。若账号和密码为“北信”和“admin”则为验证成功,将账号存入request对象中,并将请求转发给WelcomeServlet。若账号和密码验证失败,则响应给客户端如下图所示信息
    • (1)请求参数的信息;(2)客户端的信息;(3)设置Refresh响应头,10秒钟后跳转到login.html)。
  • 3.编写WelcomeServlet,取出request对象中的账号,并响应给客户端如下图所示信息。点击资源名称超链接之后,将请求发送到DownloadServlet,完成资源的下载。

代码


1. LoginServlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("admin")&&password.equals("admin")){
request.setAttribute("username", username);
RequestDispatcher rd=
request.getRequestDispatcher("WelcomeServlet");
rd.forward(request, response);
}else {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>错误信息</title></head>");
out.println("<body>");
out.println("<h3>错误提示:</h3>");
out.println("账号:"+username);
out.println("<br>");
out.println("密码:"+password);
out.println("<br/><br/>");
out.println("<hr width=99% >");
out.println("<table border='0'>");
out.println("<tr><td>客户主机名:</td>");
out.println("<td>"+request.getRemoteHost()+"</td></tr>");
out.println("<tr><td>客户IP地址:</td>");
out.println("<td>"+request.getRemoteAddr()+"</td></tr>");
out.println("<tr><td>端口:</td>");
out.println("<td>"+request.getRemotePort()+"</td></tr>");
out.println("<tr><td>请求方法:</td>");
out.println("<td>"+request.getMethod()+"</td></tr>");
out.println("</table>");
out.println("<br/><br/>");
out.println("<hr width=99% >");
out.println("<p>你的登陆信息有误<br/>十秒钟后将自动跳转到登陆界面<p>");
out.println("</body></html>");
response.setHeader("Refresh", "10;URL=http://localhost:8080/test/Login.html");
}
}'

2.WelcomeServlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getAttribute("username");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<head><title>成功登陆</title></head>");
out.println("你好,你已成功登陆!<br>欢迎选择资源下载:");
out.println("<br><br>");
out.println("<a href=DownloadServlet>《hello world!》</a>");
out.println("</body></html>");
doGet(request, response);
}

3.DownloadServlet//基本功能实现,有待完善。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置文件的内容类型
response.setContentType("application/msword;charset=gb2312");
//设置Content-Disposition响应头,指定文件名
response.setHeader("Content-Disposition",
"attachment;filename=helloworld!");
//获得输入流对象
OutputStream os = response.getOutputStream();
ServletContext context = getServletContext();
//返回输入流对象
InputStream is = context.getResourceAsStream("/hello.html");
byte[] bytearray=new byte[1024];
int bytesread=0;
while((bytesread = is.read(bytearray))!=-1){
//将数据发送到客户端
os.write(bytearray, 0, bytesread);
}
os.flush();
is.close();
}

实验总结

  • 本次实验时间仓促,只是实现了基本功能,还有些逻辑问题没有进一步优化,有待完善。
  • 完成周期过长,基本都是书上的例题,问题不断,不能运行啊,无法正常跳转到下个界面等等。。
  • 还有些问题没有解决,有待优化。
感谢捐赠~~